function typeFormatter(value, row, index)
{
    if (value == undefined || value == "") return "";
    else if (value == 0)
        return "信息录入";
    else if (value == 1)
        return "打卡(一天一次)";
    else if (value == 2)
        return "进出登记";
    else return ""
}
function iotypeFormatter(value, row, index)
{
    if (value == undefined || value == "") return "";
    else if (value == 1)
        return "进入";
    else if (value == -1)
        return "离开";
    else return ""
}
function sexFormatter(value, row, index)
{
    if (value == undefined || value == "") return "";
    else if (value == 1)return "男";
    else return "女"
}

function typeFormatter(value, row, index)
{
    if (value == undefined || value == "") return "";
    else if (value == 0)return "信息登记";
    else if (value == 1)return "健康打卡";
    else if (value == 2)return "健康检测";
    else return ""
}

function changeState(id, state) {
    $.ajax({
        type: 'POST',
        url: "changeState",
        data: {
            id: id,
            state: state
        },
        success: function () {
            $('#mytable').bootstrapTable('refresh', {url: '/admin/health/list'});
        },
        dataType: "json"
    });
}

function deleteOne(id) {
    swal({
        title: '您确定要删除此行数据？',
        text: '你将无法恢复它！',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#ed5666',
        confirmButtonText: '确定删除！',
        cancelButtonText: '取消删除！',
        confirmButtonClass: 'btn btn-success',
        cancelButtonClass: 'btn btn-danger',
        buttonsStyling: false
    }).then(function () {

        $.ajax({
            type: 'POST',
            url: "deleteOne",
            data: {
                id: id,
            },
            success: function (status) {
                if (status == 1) {
                    swal({title: "删除成功", text: "", type: "success"})
                    $('#mytable').bootstrapTable('refresh', {url: '/admin/health/list'});
                } else {
                    swal({title: "删除失败", text: "", type: "error"})
                }
            },
            dataType: "json"
        });

    }, function (dismiss) {
        // dismiss的值可以是'cancel', 'overlay',
        // 'close', 'timer'
        if (dismiss === 'cancel') {
            swal({title: '您已取消操作！', text: '', timer: 2000})
        }
    })
}

var editsw = [];

/**
 * 为模态框填充数据
 */
$('#modal-form').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget); // 触发事件的按钮
    var index = button.data('index');
    var modal = $(this);
    var data = $('#mytable').bootstrapTable("getData")[index]
    console.log(data)

    modal.find('#id').val(data.id)
    modal.find('#cardno').val(data.cardno)
    modal.find('#name').val(data.name)
    modal.find('#phone').val(data.phone)
    modal.find('#company').val(data.company)
    modal.find('#sex').val(data.sex)
    modal.find('#age').val(data.age)
    modal.find('#type').find("option[value='" + data['type'] + "']").attr("selected", true);
    modal.find('#iotype').find("option[value='" + data['iotype'] + "']").attr("selected", true);
    editsw["isleave"] == undefined ? editsw["isleave"] = new Switchery(document.querySelector('#isleave'), {color: "#1AB394"}) : ""
    if (data.isleave == 1) {
        setSwitchery(editsw["isleave"], true);
    } else {
        setSwitchery(editsw["isleave"], false);
    }
    editsw["isjiechu"] == undefined ? editsw["isjiechu"] = new Switchery(document.querySelector('#isjiechu'), {color: "#1AB394"}) : ""
    if (data.isjiechu == 1) {
        setSwitchery(editsw["isjiechu"], true);
    } else {
        setSwitchery(editsw["isjiechu"], false);
    }
    editsw["isjiangkang"] == undefined ? editsw["isjiangkang"] = new Switchery(document.querySelector('#isjiangkang'), {color: "#1AB394"}) : ""
    if (data.isjiangkang == 1) {
        setSwitchery(editsw["isjiangkang"], true);
    } else {
        setSwitchery(editsw["isjiangkang"], false);
    }
    editsw["isfare"] == undefined ? editsw["isfare"] = new Switchery(document.querySelector('#isfare'), {color: "#1AB394"}) : ""
    if (data.isfare == 1) {
        setSwitchery(editsw["isfare"], true);
    } else {
        setSwitchery(editsw["isfare"], false);
    }
    editsw["iskesou"] == undefined ? editsw["iskesou"] = new Switchery(document.querySelector('#iskesou'), {color: "#1AB394"}) : ""
    if (data.iskesou == 1) {
        setSwitchery(editsw["iskesou"], true);
    } else {
        setSwitchery(editsw["iskesou"], false);
    }
    editsw["isxmen"] == undefined ? editsw["isxmen"] = new Switchery(document.querySelector('#isxmen'), {color: "#1AB394"}) : ""
    if (data.isxmen == 1) {
        setSwitchery(editsw["isxmen"], true);
    } else {
        setSwitchery(editsw["isxmen"], false);
    }
    editsw["iszhiyu"] == undefined ? editsw["iszhiyu"] = new Switchery(document.querySelector('#iszhiyu'), {color: "#1AB394"}) : ""
    if (data.iszhiyu == 1) {
        setSwitchery(editsw["iszhiyu"], true);
    } else {
        setSwitchery(editsw["iszhiyu"], false);
    }
    editsw["isfuxie"] == undefined ? editsw["isfuxie"] = new Switchery(document.querySelector('#isfuxie'), {color: "#1AB394"}) : ""
    if (data.isfuxie == 1) {
        setSwitchery(editsw["isfuxie"], true);
    } else {
        setSwitchery(editsw["isfuxie"], false);
    }
    modal.find('#tiwen').val(data.tiwen)
    modal.find('#statday').val(data.statday)
    modal.find('#address').val(data.address)
    modal.find('#longitude').val(data.longitude)
    modal.find('#latitude').val(data.latitude)
    modal.find('#mpid').val(data.mpid)
    modal.find('#sname').val(data.sname)

})

//tableHeight函数
function tableHeight() {
    //可以根据自己页面情况进行调整
    return $(window).height() - 200;
}

$(function () {

    var sw = [];
    $(".js-switch").each(function (i, e) {
        sw[i] = new Switchery(e, {color: "#1AB394"})
    })
    $(window).resize(function () {
        $('#mytable').bootstrapTable('resetView', {
            height: tableHeight()
        })
    })

    $('#export_btn').click(function () {
        var p = {
            search: $('#search').val(),
            type: $('#healthtype').val() || "",
            storeid: $('#storeid').val() || "",
            statday: $('#statday').val() || "",
            ts: new Date().getTime()
        };
        var form = $("<form>");
        form.attr('style', 'display:none');
        form.attr('target', '');
        form.attr('method', 'post');
        form.attr('action', 'export');

        var input1 = $('<input>');
        input1.attr('type', 'hidden');
        input1.attr('name', 'type');
        input1.attr('value', p.type);
        form.append(input1);

        var input2 = $('<input>');
        input2.attr('type', 'hidden');
        input2.attr('name', 'search');
        input2.attr('value', p.search);
        form.append(input2);

        var input3 = $('<input>');
        input3.attr('type', 'hidden');
        input3.attr('name', 'storeid');
        input3.attr('value', p.storeid);
        form.append(input3);

        var input4 = $('<input>');
        input4.attr('type', 'hidden');
        input4.attr('name', 'statday');
        input4.attr('value', p.statday);
        form.append(input4);

        $('body').append(form);

        form.submit();
        form.remove();

    })

    // $('#statday').datepicker(dateformat);
    $(".datepicker").datepicker({
        language: "zh-CN",
        autoclose: true,//选中之后自动隐藏日期选择框
        clearBtn: true,//清除按钮
        todayBtn: true,//今日按钮
        format: "yyyy-mm-dd"//日期格式，详见 http://bootstrap-datepicker.readthedocs.org/en/release/options.html#format
    });

    $('.datepicker').datepicker('setDate', new Date());
    //生成用户数据
    $('#mytable').bootstrapTable({
        method: 'get',
        url: "/admin/health/list",
        // height:520,//高度调整
        // toolbar: '#toolbar',
        striped: true, //是否显示行间隔色
        dataField: "records",
        pagination: true,//是否分页
        queryParamsType: 'limit',
        queryParams: queryParams,
        sidePagination: 'server',
        pageNumber: 1, //初始化加载第一页，默认第一页
        pageSize: 50,//单页记录数
        pageList: [50, 100, 200, 500],//分页步进值
        showExport: true,//显示导出
        showRefresh: false,//刷新按钮
        showColumns: false,
        clikToSelect: true,//是否启用点击选中行
        // toolbarAlign:'right',
        // buttonsAlign:'right',//按钮对齐方式
        columns: [
            {
                title: '全选',
                field: 'select',
                checkbox: true,
                width: 25,
                align: 'center',
                valign: 'middle'
            },
            {
                title: 'ID',
                field: 'id',
                visible: false
            },
            {
                title: '统计日期',
                field: 'statday'
            },
            {
                title: '姓名',
                field: 'name'
            },
            {
                title: '性别',
                field: 'sex',
                formatter: "sexFormatter"
            },
            {
                title: '年龄',
                field: 'age'
            },
            {
                title: '联系电话',
                field: 'phone'
            },
            {
                title: '现住址',
                field: 'address'
            },
            {
                title: '身份证号码',
                field: 'cardno'
            },
            {
                title: '公司单位',
                field: 'company'
            },
            {
                title: '体温',
                field: 'tiwen'
            },
            {
                title: '近期外出',
                field: 'isleave',
                formatter: "sfFormatter"
            },
            {
                title: '是否接触病人',
                field: 'isjiechu',
                formatter: "sfFormatter"
            },
            {
                title: '统计类型',
                field: 'type',
                formatter: "typeFormatter"
            },
            {
                title: '进出类型',
                field: 'iotype',
                formatter: "iotypeFormatter"
            },
            // {
            //     title: '是否健康',
            //     field: 'isjiangkang',
            //     formatter: "sfFormatter"
            // },
            // {
            //     title: '是否发热',
            //     field: 'isfare',
            //     formatter: "sfFormatter"
            // },
            // {
            //     title: '是否咳嗽',
            //     field: 'iskesou',
            //     formatter: "sfFormatter"
            // },
            // {
            //     title: '是否胸闷',
            //     field: 'isxmen',
            //     formatter: "sfFormatter"
            // },
            // {
            //     title: '是否治愈',
            //     field: 'iszhiyu',
            //     formatter: "sfFormatter"
            // },
            // {
            //     title: '是否腹泻乏力',
            //     field: 'isfuxie',
            //     formatter: "sfFormatter"
            // },
            {
                title: '检测机构',
                field: 'sname'
            },
            {
                title: '打卡经度',
                field: 'longitude'
            },
            {
                title: '打卡维度',
                field: 'latitude'
            },
            {
                title: '操作',
                field: 'Attribute',
                align: 'center',
                width: 170,
                formatter: actionFormatter
            }
        ],
        locale: 'zh-CN',//中文支持,
    })
    //请求服务数据时所传参数
    function queryParams(params) {
        var p = {
            limit: params.limit,
            offset: params.offset,
            sort: params.sort,
            order: params.order,
            search: $('#search').val() || "",
            type: $('#healthtype').val() || "",
            storeid: $('#storeid').val() || "",
            statday: $('#statday').val() || ""
        }
        return p;
    }
    $('#search_btn').click(function () {
        var p = {
            search: $('#search').val(),
            type: $('#healthtype').val() || "",
            storeid: $('#storeid').val() || "",
            statday: $('#statday').val() || "",
            ts: new Date().getTime()
        };
        $('#mytable').bootstrapTable('refresh', p);
    })
    /*
     * 验证错误提示信息高亮
     */
    $.validator.setDefaults({
        highlight: function (e) {
            $(e).closest(".form-group").removeClass("has-success").addClass("has-error")
        }, success: function (e) {
            e.closest(".form-group").removeClass("has-error").addClass("has-success")
        }, errorElement: "span", errorPlacement: function (e, r) {
            e.appendTo(r.is(":radio") || r.is(":checkbox") ? r.parent().parent().parent() : r.parent())
        }, errorClass: "help-block m-b-none", validClass: "help-block m-b-none"
    }), $("#aform").validate(), $("#mform").validate();
})

function checkAdd() {
    $('input:checkbox').each(function (index, item) {
        if ($(this).prop("checked")) {
            $(this).val(1);
            console.log($(this).attr("name") + "=" + $(this).val())
        } else {
            $(this).val(-1);
            console.log($(this).attr("name") + "=" + $(this).val())
        }
    });

    return true;
}

function checkModify() {
    $('input:checkbox').each(function (index, item) {
        if ($(this).prop("checked")) {
            $(this).val(1);
            console.log($(this).attr("id") + "=" + $(this).val())
        } else {
            $(this).val(-1);
            console.log($(this).attr("id") + "=" + $(this).val())
        }
    });

    return true;
}